#include<bits/stdc++.h>
using std::cin;
using std::cout;
using i32 = int;
using u32 = unsigned int;
using i64 = long long;
using u64 = unsigned long long;
using pii = std::pair<int, int>;
#define TESTS int _; std::cin >> _; while(_--)
#define all(a) a.begin(), a.end()
#define all1(a) a.begin() + 1, a.end()
#define pb push_back
#define fi first
#define se second
#define SUM(v) std::accumulate(all(v), 0LL)
#define SUM1(v) std::accumulate(all1(v), 0LL)
#define MIN(v) *std::min_element(all(v))
#define MIN1(v) *std::min_element(all1(v))
#define MAX(v) *std::max_element(all(v))
#define MAX1(v) *std::max_element(all1(v))
#define UNIQUE(v) std::sort(all(v)), v.erase(std::unique(all(v), v.end()))
#define UNIQUE1(v) std::sort(all1(v)), v.erase(std::unique(all1(v), v.end()))

constexpr i64 inf = 1E18, Md3 = 998244353, Md7 = 1e9 + 7;
constexpr double eps = 1E-9;
// std::mt19937 rng {std::chrono::steady_clock::now().time_since_epoch().count()};

int main() {
	
	std::ios::sync_with_stdio(false);
	std::cin.tie(0);
		
	int n;
	cin >> n;
	std::vector<i64> a(n);
	for(int i = 0; i < n; i++) {
		cin >> a[i];
	}
	std::vector dp(n + 1, std::vector<i64>(2, -inf));
	dp[0][0] = 0;
	for(int i = 0; i < n; i++) {
		dp[i + 1][0] = std::max(dp[i + 1][0], dp[i][0]);
		dp[i + 1][1] = std::max(dp[i + 1][1], dp[i][1]);
		dp[i + 1][1] = std::max(dp[i + 1][1], dp[i][0] + 1LL * a[i]);
		dp[i + 1][0] = std::max(dp[i + 1][0], dp[i][1] + 2LL * a[i]);
	}
	cout << std::max(dp[n][0], dp[n][1]);

	return 0;
}